package com.blog.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.admin.service.CommentService;
import com.blog.common.core.utils.PageUtils;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.entity.Comment;
import com.blog.common.domain.dto.CommentDto;
import com.blog.common.domain.vo.CommentVo;
import com.blog.common.mapper.dao.CommentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author Valerie
 * @since 2023-03-11
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public TableDataInfo<CommentVo> listComments(CommentDto commentDto) {
        Page<Comment> page = commentMapper.selectCommentsList(new Page<>(PageUtils.getPageNum(), PageUtils.getPageSize()), commentDto);
        List<CommentVo> commentVoList = BeanUtil.copyToList(page.getRecords(), CommentVo.class);
        return new TableDataInfo<>(commentVoList, page.getTotal());
    }

    @Override
    public Boolean auditComment(Serializable id, Boolean state) {
        return new LambdaUpdateChainWrapper<>(commentMapper)
                .eq(Comment::getId, id)
                .set(Comment::getIsReview, state ? 1 : 3)
                .update();
    }

    @Override
    public void removeComment(String commentIds) {
        if (StringUtils.isNotBlank(commentIds)) {
            String[] ids = commentIds.trim().split(",");
            log.info("删除评论 id：{}",  commentIds);
            this.remove(new LambdaQueryWrapper<Comment>().in(Comment::getId, Arrays.asList(ids)));
        }
    }
}
